﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedHouse.Data.Service;
using RedHouse.Data.Common;

namespace RedHouse.Data.Base
{
    public class DetailService : IDetailService
    {
        #region Declaration
        private NewRedHouseEntitiesDataContext _dataContext;
        #endregion

        public DetailService()
        {
            _dataContext = new NewRedHouseEntitiesDataContext();
        }

        #region IDetailService Members

        public OperationResult Save(Detail detail)
        {
            OperationResult result = new OperationResult()
            {
                Result = true,
                Message = string.Empty
            };
            try
            {
                _dataContext.Details.InsertOnSubmit(detail);
                _dataContext.SubmitChanges();
                result.ReturnObject = detail;
            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = ex.Message;
            }
            return result;
        }

        public Detail GetDetailByAssociatedID(Guid associatedId)
        {
            return _dataContext.Details.Single(d => d.AssociatedID == associatedId);
        }

        public OperationResult Delete(Detail detail)
        {
            OperationResult result = new OperationResult()
            {
                Result = true,
                Message = string.Empty
            };
            try
            {
                _dataContext.Details.DeleteOnSubmit(detail);
                _dataContext.SubmitChanges();
                result.ReturnObject = detail;
            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = ex.Message;
            }
            return result;
        }

        #endregion
    }
}
